export default {
  formatClassHour(classHour) {
    if (classHour) {
      return classHour + " / " + (classHour / 2).toFixed();
    }
    return "-";
  },
  formatWeekDate(dateStr) {
    if (dateStr) {
      const date = new Date(dateStr);
      const day = date.getDay();
      return dateStr + " (" + this.formatDay(day) + ")";
    }
    return "-";
  },
  formatDay(day) {
    const dayText = ["日", "一", "二", "三", "四", "五", "六"];
    return dayText[day];
  },
  formatCost(cost) {
    if (cost) {
      return cost.toFixed(2);
    }
    return "-";
  },
  isSameDay(day1, day2) {
    return (
      day1.getFullYear() === day2.getFullYear() &&
      day1.getMonth() === day2.getMonth() &&
      day1.getDate() === day2.getDate()
    );
  },
  compareDay(day1, day2) {
    if (day1.getFullYear() === day2.getFullYear()) {
      if (day1.getMonth() === day2.getMonth()) {
        if (day1.getDate() === day2.getDate()) {
          return 0;
        } else {
          return day1.getDate() > day2.getDate() ? 1 : -1;
        }
      } else {
        return day1.getMonth() > day2.getMonth() ? 1 : -1;
      }
    } else {
      return day1.getFullYear() > day2.getFullYear() ? 1 : -1;
    }
  },
  pickWeekInRange(start, end, week) {
    const dateColumns = [];
    do {
      if (start.getDay() === week) {
        dateColumns.push(new Date(start));
      }
      start.setDate(start.getDate() + 1);
    } while (this.compareDay(start, end) <= 0);
    return dateColumns;
  },
  formatTitle(dateObj) {
    const m = dateObj.getMonth() + 1;
    const month = m > 9 ? m : "0" + m;
    const d = dateObj.getDate();
    const date = d > 9 ? d : "0" + d;
    return month + "/" + date;
  },
  formatStandardDate(dateObj, separator) {
    const s = separator !== undefined ? separator : "";

    const year = dateObj.getFullYear();
    const m = dateObj.getMonth() + 1;
    const month = m > 9 ? m : "0" + m;
    const d = dateObj.getDate();
    const date = d > 9 ? d : "0" + d;
    return year + s + month + s + date;
  },
  getQueryString(name) {
    const index = window.location.hash.indexOf("?");
    const query = window.location.hash.substr(index + 1);
    const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    const r = query.match(reg);
    if (r !== null) {
      return decodeURI(r[2]);
    }
    return null;
  },
  getAuthorization() {
    return "Bearer " + localStorage.getItem("Authorization");
  },
  removeAuthorization() {
    localStorage.removeItem("Authorization");
  }
};
